<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>学校管理员登录</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <style>
        .login-third {
            margin-top: 3rem;
        }
    </style>
</head>

<body>
    <section class="aui-content aui-margin-t-15">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-mobile"></i>
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" id="loginNameInput" placeholder="登录名">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-lock"></i>
                    </div>
                    <div class="aui-list-item-input">
                        <input type="password" id="passwordInput" placeholder="密码">
                    </div>
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-display" id="display-hide-icon" onclick="toggleDisplay()"></i>
                    </div>
                </div>
            </li>
        </ul>
    </section>
    <section class="aui-content-padded">
        <div class="aui-btn aui-btn-block aui-btn-info aui-btn-sm" tapmode onclick="login()">登录</div>
    </section>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    var loginNameInput;
    var passwordInput;
    apiready = function() {
        api.parseTapmode();
        loginNameInput = $api.byId('loginNameInput');
        passwordInput = $api.byId('passwordInput');
    };

    function login() {
        var loginName = loginNameInput.value;
        var password = passwordInput.value;
        if (loginName.trim().length === 0 || password.trim().length === 0) {
            api.toast({
                msg: '登录名或密码不能为空',
                duration: 2000,
                location: 'bottom'
            });
            return;
        }
        doLogin(loginName, password);
    }
    function clearInput(){
      loginNameInput.value = '';
      passwordInput.value = '';
    }
    function doLogin(loginName, password) {
        // 提交JSON数据
        api.ajax({
            url: $baseUrl + 'login',
            method: 'post',
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            },
            data: {
                body: {
                    loginName: loginName,
                    password: password
                }
            }
        }, function(ret, err) {
            if (ret) {
                if (ret.code === 200) {
                    api.toast({
                        msg: '登陆成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    clearInput();
                    $api.setStorage('editor', ret.data);
                    $api.setStorage('school', ret.data.school);
                    api.openWin({
                        name: 'home',
                        url: './home.html'
                    });
                    api.sendEvent({
                        name: 'updateStorage'
                    });
                    api.closeWin();
                } else if (ret.code === 1003) {
                    api.toast({
                        msg: '用户名或密码错误!',
                        duration: 2000,
                        location: 'bottom'
                    });
                    clearInput();
                } else {
                    api.toast({
                        msg: '登录失败，请重试',
                        duration: 2000,
                        location: 'bottom'
                    });
                    clearInput();
                }
            } else {
                api.toast({
                    msg: '请求异常!',
                    duration: 2000,
                    location: 'bottom'
                });
                clearInput();
            }
        });
    }

    function toggleDisplay() {
        var iconEl = $api.byId('display-hide-icon');
        var passwordInput = $api.byId('passwordInput');

        if ($api.hasCls(iconEl, 'aui-icon-display')) {
            $api.removeCls(iconEl, 'aui-icon-display');
            $api.addCls(iconEl, 'aui-icon-hide');
            $api.attr(passwordInput, 'type', 'text');
        } else {
            $api.removeCls(iconEl, 'aui-icon-hide');
            $api.addCls(iconEl, 'aui-icon-display');
            $api.attr(passwordInput, 'type', 'password');
        }
    }
</script>

</html>
